package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.ModifyLogsBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IModifyService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 调休（加班）额度调整
 * @author 张鑫
 * 2018-01-31
 *
 */
@Controller
@RequestMapping("/modify")
public class ModifyController {

	@Resource
	private IModifyService modifyService;
	
	/**
	 * 查询员工调休剩余额度
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRestBalanceList.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getRestBalanceList(HttpServletRequest request, @RequestBody ModifyLogsBean modifyLogsBean){
		//获取登录用户
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null ) {
			throw new ResultFaultException("1_00001");
		}
		if (modifyLogsBean.getPageCount()==null ||modifyLogsBean.getPageNo()==null) {
			throw new ResultFaultException("0_00001");
		}
		int pageNo=modifyLogsBean.getPageNo();
		int pageCount=modifyLogsBean.getPageCount();
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		modifyLogsBean.setStartNum(startNum);
		return ResultMap.success(modifyService.getRestBalanceList(modifyLogsBean));
	}
	
	/**
	 * 查询调整调休额度logs
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getModifyLogsList.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object getModifyLogsList(HttpServletRequest request){
		//获取登录用户
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		String employeeNo = request.getParameter("employeeNo");
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		if (StringUtils.isBlank(employeeNo)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(modifyService.getModifyLogsList(employeeNo));
	}
	
	/**
	 * 设置调休余额；并新增logs
	 * @param request
	 * @param modifyLogsBean
	 * @return
	 */
	@RequestMapping(value = "/saveModifyLogs.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object saveModifyLogs(HttpServletRequest request, @RequestBody ModifyLogsBean modifyLogsBean){
		//获取登录用户
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		if (modifyLogsBean.getAfterBalanceHoursDay()==null
				||modifyLogsBean.getEmployeeId()==null
				||StringUtils.isBlank(modifyLogsBean.getEmployeeNo())
				||StringUtils.isBlank(modifyLogsBean.getChineseName())
				||modifyLogsBean.getMinutes()==null) {
			throw new ResultFaultException("0_00001");
		}
		modifyLogsBean.setModifyEmployeeId(userEmployee.getEmployeeId());
		return ResultMap.success(modifyService.saveModifyLogs(modifyLogsBean));
	}
}
